package com.saudidentalapp;

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.saudiapp.webservice.Constants;
import com.saudiapp.webservice.Utilities;
import com.saudidentalapp.SearchAdapter.DataListener;

public class SearchActivity extends Activity 
{
	
	ListView lstdata;
	Utilities utilities;
	SearchActivity activity;
	TextView txtDataStatus;
	
	EditText edtSearchBox;
	SearchAdapter adapter;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_search);
		
		activity=SearchActivity.this;
		lstdata=(ListView)findViewById(R.id.xlstdata);
		txtDataStatus=(TextView)findViewById(R.id.xtxtDataStatus);
		//edtSearchBox=(EditText)findViewById(R.id.xedtSearchBox);
		
		
		setView();
		
		
		
		
		edtSearchBox.addTextChangedListener(new TextWatcher() {
			
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) 
			{
				if(adapter!=null)
					SearchActivity.this.adapter.getFilter().filter(s);
				
			}
			
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void afterTextChanged(Editable s) 
			{
				
				
			}
		});			
	}
	
	void setView()
	{
		if(Constants.Data.size()>0)
		{
			adapter=new SearchAdapter(activity, Constants.Data);
			lstdata.setAdapter(adapter);
			lstdata.setVisibility(View.VISIBLE);
			txtDataStatus.setVisibility(View.GONE);	
			
			adapter.setOnDataListener(new DataListener() 
			{
				
				@Override
				public void onResult(boolean Result) 
				{
					if(Result)
					{
						txtDataStatus.setVisibility(View.GONE);
					}
					else
					{
						txtDataStatus.setVisibility(View.VISIBLE);
					}
					
				}
			});
		}
		else
		{
			txtDataStatus.setVisibility(View.VISIBLE);
		}
		
		
		
	}
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		setView();
	}
}
